import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/manager/xindong_hello_ctr.dart';
import 'package:xindong/manager/xindong_audio_ctr.dart';
import 'package:xindong/manager/xindong_page_singleton.dart';
import 'package:xindong/models/user/xindong_home_user.dart';
import 'package:xindong/res/assets_res.dart';
import 'package:xindong/utils/xindong_image_util.dart';
import 'package:xindong/utils/xindong_text_util.dart';
import 'package:xindong/widgets/common/xindong_common_dot.dart';
import 'package:xindong/widgets/xindong_avatar.dart';
import 'package:xindong/widgets/xindong_biaoqian_view.dart';
import 'package:svgaplayer_flutter/player.dart';

class XinDongUserListCell extends StatelessWidget {
  final bool isSearch;
  final XinDongHomeUser model;

  const XinDongUserListCell({Key? key, required this.model, this.isSearch = false}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => XinDongPageSingleton.instance.jumpToSpace(model),
      behavior: HitTestBehavior.opaque,
      child: Container(
        color: Colors.transparent,
        padding: const EdgeInsets.only(left: kPaddingValue, right: kPaddingValue, top: 5, bottom: 5),
        child: Stack(
          alignment: Alignment.center,
          children: [
            Row(
              children: [
                Stack(
                  alignment: Alignment.center,
                  fit: StackFit.loose,
                  clipBehavior: Clip.none,
                  children: [
                    XinDongAvatar(url: model.user_head, size: 71),
                    Positioned(
                      top: 4,
                      left: 4,
                      child: ImageUtil.asset(model.is_online == 1 ? AssetsRes.ICON_ONLINE : AssetsRes.ICON_OFFLINE,
                          width: 12),
                    ),
                    if (model.is_new == 1)
                      Positioned(
                        bottom: 0,
                        child: ImageUtil.asset(AssetsRes.LABEL_NEW_PEOPLE, height: 16),
                      ),
                  ],
                ),
                const SizedBox(width: 10),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      SizedBox(
                        height: 22,
                        width: double.infinity,
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Text(
                              model.user_name ?? "",
                              maxLines: 1,
                              overflow: TextOverflow.ellipsis,
                              softWrap: true,
                              style: TextStyle(
                                color: model.isHuiYuan ? Colors.red : DZColors.titleColor,
                                fontSize: 16,
                                fontWeight: FontWeight.w600,
                                height: 1.2,
                              ),
                            ),
                            const SizedBox(width: 2),
                            XinDongLabelsHomeList(user: model),
                          ],
                        ),
                      ),
                      const SizedBox(height: 4),
                      Text(
                        "${model.city ?? "未知"}｜${model.user_age == "未知" ? "未知" : "${model.user_age}岁"}${TextUtil.isEmpty(model.statureText) ? "" : " | ${model.statureText}"}${TextUtil.isEmpty(model.occupation) ? "" : ""
                            " | ${model.occupation}"}",
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: const TextStyle(fontSize: 12, color: DZColors.contentColor),
                      ),
                      const SizedBox(height: 4),
                      Text(
                        "ID: ${model.uid}",
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: const TextStyle(fontSize: 12, color: DZColors.contentColor),
                      ),
                      const SizedBox(height: 4),
                      isSearch
                          ? Padding(
                              padding: const EdgeInsets.only(top: 5.0),
                              child: Text(
                                model.register_datetime ?? "",
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                                style: const TextStyle(fontSize: 14, color: DZColors.contentColor),
                              ),
                            )
                          : TextUtil.isEmpty(model.voice)
                              ? Padding(
                                  padding: const EdgeInsets.only(top: 5.0),
                                  child: Text(
                                    model.self_slogan ?? "",
                                    maxLines: 1,
                                    overflow: TextOverflow.ellipsis,
                                    style: const TextStyle(fontSize: 14, color: DZColors.contentColor),
                                  ),
                                )
                              : voiceWidget(),
                    ],
                  ),
                ),
              ],
            ),
            Positioned(
              right: 0,
              child: GetBuilder<XinDongHelloCtr>(
                id: "${model.user_id}",
                builder: (controller) {
                  return GestureDetector(
                    onTap: () => XinDongHelloCtr.to.onSayHelloUser(model),
                    behavior: HitTestBehavior.opaque,
                    child: ImageUtil.asset(
                      model.canSayHello == true ? AssetsRes.BUTTON_DASHAN : AssetsRes.BUTTON_SIXIN,
                      width: 60,
                      height: 30,
                    ),
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget voiceWidget() {
    return GetBuilder<XinDongAudioCtr>(builder: (controller) {
      bool isPlaying = controller.isPlaying && model.voice == controller.playUrl;
      return GestureDetector(
        onTap: () => controller.play(model.voice ?? ""),
        behavior: HitTestBehavior.opaque,
        child: Container(
          height: 26,
          padding: const EdgeInsets.fromLTRB(8, 3, 8, 3),
          decoration: BoxDecoration(
            border: Border.all(width: 0.5, color: DZColors.placeholderColor),
            borderRadius: BorderRadius.circular(20),
          ),
          child: Row(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              SizedBox(
                width: 60,
                child: isPlaying
                    ? const SVGASimpleImage(assetsName: AssetsRes.VOICE)
                    : ImageUtil.asset(AssetsRes.IMAGE_AUDIO_PLAY_PLACEHOLDER),
              ),
              const SizedBox(width: 5),
              Text(
                "${model.voice_time}",
                style: const TextStyle(fontSize: 12, color: DZColors.placeholderColor, height: 1.0),
              ),
            ],
          ),
        ),
      );
    });
  }
}
